package cn.dduan.fool.design.pattern.behavior.handler;

/**
 * 责任链模式
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
public class HandlerPattern {
    public static void main(String[] args) {
        // 辅导员
        Leader instructor = new Instructor("陈毅");
        // 系主任
        Leader departmentHead = new DepartmentHead("王明");
        // 院长
        Leader dean = new Dean("张强");
        // 校长
        Leader president = new President("王晗");
        // 辅导员的后续者是系主任
        instructor.setSuccessor(departmentHead);
        // 系主任的后续者是院长
        departmentHead.setSuccessor(dean);
        // 院长的后续者是校长
        dean.setSuccessor(president);

        // 请假3天的请假条
        LeaveNode leaveNode1 = new LeaveNode("张三", 3);
        instructor.handleRequest(leaveNode1);

        // 请假9天的请假条
        LeaveNode leaveNode2 = new LeaveNode("李四", 9);
        instructor.handleRequest(leaveNode2);

        // 请假15天的请假条
        LeaveNode leaveNode3 = new LeaveNode("王五", 15);
        instructor.handleRequest(leaveNode3);

        // 请假20天的请假条
        LeaveNode leaveNode4 = new LeaveNode("赵六", 20);
        instructor.handleRequest(leaveNode4);

    }
}

/**
 * 请假条：
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
class LeaveNode {
    /**
     * 请假天数
     **/
    private int number;

    /**
     * 请假人
     **/
    private String person;

    public LeaveNode(String person, int number) {
        this.person = person;
        this.number = number;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getPerson() {
        return person;
    }

    public void setPerson(String person) {
        this.person = person;
    }
}

/**
 * 抽象处理者
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
abstract class Leader {
    /**
     * 姓名
     */
    public String name;

    /**
     * 后继者
     */
    protected Leader successor;

    public Leader(String name) {
        this.name = name;
    }

    public void setSuccessor(Leader successor) {
        this.successor = successor;
    }

    /**
     * 后继者
     *
     * @param leaveNode
     * @return void
     * @author dduan
     * @version 1.0
     * @date 2023/4/10
     */
    public abstract void handleRequest(LeaveNode leaveNode);
}

/**
 * 具体实现：辅导员
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
class Instructor extends Leader {

    public Instructor(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveNode leaveNode) {
        if (leaveNode.getNumber() <= 3) {
            // 小于3天辅导员审批
            System.out.println("辅导员" + name + "审批" + leaveNode.getPerson() + "同学的请假条,请假天数为" + leaveNode.getNumber() + "天。");
        } else {
            // 否则传递给系主任
            if (this.successor != null) {
                this.successor.handleRequest(leaveNode);
            }
        }
    }
}

/**
 * 系主任
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
class DepartmentHead extends Leader {

    public DepartmentHead(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveNode leaveNode) {
        if (leaveNode.getNumber() <= 7) {
            // 小于7天系主任审批
            System.out.println("系主任" + name + "审批" + leaveNode.getPerson() + "同学的请假条,请假天数为" + leaveNode.getNumber() + "天。");
        } else {
            // 否则传递给院长
            if (this.successor != null) {
                this.successor.handleRequest(leaveNode);
            }
        }
    }
}

/**
 * 院长
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
class Dean extends Leader {

    public Dean(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveNode leaveNode) {
        if (leaveNode.getNumber() <= 10) {
            // 小于10天院长审批
            System.out.println("院长" + name + "审批" + leaveNode.getPerson() + "同学的请假条,请假天数为" + leaveNode.getNumber() + "天。");
        } else {
            // 否则传递给校长
            if (this.successor != null) {
                this.successor.handleRequest(leaveNode);
            }
        }
    }
}

/**
 * 校长
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
class President extends Leader {

    public President(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveNode leaveNode) {
        if (leaveNode.getNumber() <= 15) {
            // 小于15天校长长审批
            System.out.println("校长" + name + "审批" + leaveNode.getPerson() + "同学的请假条,请假天数为" + leaveNode.getNumber() + "天。");
        } else {
            // 否则不允批准
            System.out.println("请假天天超过15天,不批准...");
        }
    }
}

